//
//  FWPhotoBrowserView.m
//  SinaWebo
//
//  Created by wwj on 16/6/21.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "FWPhotoBrowserView.h"
#import "PhotoBrowerCell.h"
#import "FWGroupModel.h"
#import <Masonry.h>

@interface FWPhotoBrowserView ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>
@property (weak, nonatomic) UICollectionView *collectionView;
@property (weak, nonatomic) UILabel *countLable;
@end

@implementation FWPhotoBrowserView
static NSString *const ID = @"PhotoBrowerCell";

- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
    }
    return self;
}

- (UILabel *)countLable{
    if (_countLable == nil) {
        UILabel *countLable = [[UILabel alloc] init];
        countLable.textAlignment = NSTextAlignmentCenter;
        countLable.textColor = [UIColor whiteColor];
        countLable.backgroundColor = MyColorA(0, 0, 0, 0.1);
        [self addSubview:countLable];
        _countLable = countLable;
    }
    return _countLable;
}

- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = self.size;
        flowLayout.minimumLineSpacing = 0.0f;
        flowLayout.minimumInteritemSpacing = 0.0f;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PhotoBrowerCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
        collectionView.backgroundColor = [UIColor clearColor];
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.pagingEnabled = YES;
        collectionView.dataSource = self;
        collectionView.delegate = self;
        [self addSubview:collectionView];
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (void)setFrame:(CGRect)frame{
    frame = CGRectMake(0, 0, kScreenW, kScreenH);
    [super setFrame:frame];
}

- (void)show:(UIView *)view{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:view.tag inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    UIWindow *windown = [UIApplication sharedApplication].keyWindow;
    [windown addSubview:self];
    
    FWGroupModel *model = self.photos[view.tag];
    model.isAnimtion = TRUE;
    
    [self.collectionView reloadData];
    [UIApplication sharedApplication].statusBarHidden = YES;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    [self.countLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.bottom.mas_equalTo(self.mas_bottom);
        make.height.mas_equalTo(44);
    }];
}

- (void)setPhotos:(NSArray *)photos{
    _photos = photos;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.photos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PhotoBrowerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.model = self.photos[indexPath.row];
    cell.superView = self;
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger page = scrollView.contentOffset.x / scrollView.width + 1;
    if (page <1) return;
    _countLable.text = [NSString stringWithFormat:@"%zd/%zd", page, self.photos.count];
}

@end
